package io.lkt.task.handler;

import io.lkt.task.bus.mapper.log.PushDetailLogMapper;
import io.lkt.task.bus.mapper.log.PushHeadLogMapper;
import io.lkt.task.bus.model.UserInfo;
import io.lkt.task.bus.model.log.PushHeadLog;
import io.lkt.task.bus.service.IUserService;
import io.lkt.task.task.async.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
public class CallHandler {

    @Autowired
    private IUserService userService;

    @Autowired
    private UploadTempMsgPushAsyncTask uploadTempMsgPushAsyncTask;

    @Autowired
    private PushHeadLogMapper pushHeadLogMapper;

    @Autowired
    private PushDetailLogMapper pushDetailLogMapper;

    //接口项目调用,20210615 发送“上传成功通知”
    @PostMapping(value = "/uploadMsgPush", produces = "application/json;charset=UTF-8")
    public String UploadMsgPush(@RequestHeader("userId") String userId, @RequestHeader("profile") String profile) {
        UserInfo userInfo = userService.queryUserInfo(Integer.valueOf(userId));
        uploadTempMsgPushAsyncTask.execute(userInfo, profile, 0);
        return "uploadMsgPush调用完成";
    }

    //后台项目调用,20210615 发送“上传成功通知” (测试)
    @CrossOrigin
    @PostMapping(value = "/UploadMsgPushBrandIdTest", produces = "application/json;charset=UTF-8")
    public String UploadMsgPushBrandIdTest(@RequestHeader("userId") String userId, @RequestHeader("profile") String profile, @RequestHeader("brandId") Integer brandId, @RequestHeader("articleId") Integer articleId) {
        UserInfo userInfo = userService.queryUserInfo(Integer.valueOf(userId));
        uploadTempMsgPushAsyncTask.executeBrandId(userInfo, profile, 0, brandId, articleId);
        return "UploadMsgPushBrandIdTest调用完成";
    }

    //后台项目调用,20210615 发送“上传成功通知”
    @CrossOrigin
    @PostMapping(value = "/UploadMsgPushBrandId", produces = "application/json;charset=UTF-8")
    public String UploadMsgPushBrandId(@RequestHeader("profile") String profile, @RequestHeader("brandId") Integer brandId, @RequestHeader("articleId") Integer articleId) throws InterruptedException {

        PushHeadLog pushHeadLog = new PushHeadLog();
        pushHeadLog.setPushName("品牌文章上架推送");
        pushHeadLog.setMsgType(14);
        pushHeadLog.setMsgTypeName("品牌文章上架推送");
        pushHeadLog.setSumCount(0);
        pushHeadLog.setCreateTime(new Date());
        pushHeadLog.setGzhCode(profile);

        pushHeadLogMapper.addPushHeadLog(pushHeadLog);
        Integer headId = pushHeadLog.getId();

        List<UserInfo> userList = userService.getUserListByNoActivation(profile);
        for (UserInfo userInfo : userList) {
            uploadTempMsgPushAsyncTask.execute(userInfo, profile, headId);
        }
        return headId.toString();
    }

    @CrossOrigin
    @PostMapping(value = "/updatePushHeadLog", produces = "application/json;charset=UTF-8")
    public String updatePushHeadLog(@RequestHeader("headId") Integer headId) throws InterruptedException {

        PushHeadLog pushHeadLogU = new PushHeadLog();
        pushHeadLogU.setId(headId);
        pushHeadLogU.setSuccessCount(pushDetailLogMapper.getSuccessCount(headId));
        pushHeadLogU.setErrCount(pushDetailLogMapper.getErrCount(headId));
        pushHeadLogU.setUpdateTime(new Date());
        pushHeadLogU.setEndTime(new Date());

        pushHeadLogMapper.updatePushHeadLog(pushHeadLogU);
        return headId.toString();
    }
}
